﻿using BloodApp.Common;
using BloodApp.Model;
using BloodApp.TCPCommunication;
using CommunityToolkit.Mvvm.ComponentModel;
using CommunityToolkit.Mvvm.Input;
using Microsoft.VisualBasic.ApplicationServices;
using SqlSugar;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using static System.Windows.Forms.VisualStyles.VisualStyleElement;

namespace BloodApp.ViewModel
{
    public partial class MainWindowVM : ObservableObject
    {
        [ObservableProperty]
        string strYY = "";

        public MainWindowVM()
        {
            AllUser = new ObservableCollection<T_User>(DbHelperService.DbContext.Queryable<T_User>().Includes(c => c.Orders).ToList());
            LogService.Logger.Info("日志——消息");
            LogService.Logger.Warn("日志——告警");
            LogService.Logger.Error("日志——错误");
            LogService.Logger.Debug("日志——调试");


            LogService.SilentLogger.Debug("日志——调试");

        }

        [ObservableProperty]
        ObservableCollection<T_User> allUser = new();
        TcpService tcpService = new TcpService();

        bool isZh = true;
        [RelayCommand]
        public void OperaAction(string opera)
        {
            if (opera == "ChangeLanguage")
            {
                if (isZh)
                    GlobalService.ChangeLanguageService.ChangeLanguagePublish("en");
                else GlobalService.ChangeLanguageService.ChangeLanguagePublish("zh");
                isZh = !isZh;
            }
            else if (opera == "CreateTable")
                DbHelperService.CreateTable();
            else if (opera == "Query")
            {
                AllUser = new ObservableCollection<T_User>(DbHelperService.DbContext.Queryable<T_User>().Includes(c => c.Orders).ToList());
            }
            else if (opera == "ManyQuery")
            {
                var result = DbHelperService.DbContext.Queryable<T_User>().Where(c => c.UserId == 1).Includes(c => c.Orders).ToList();
                //2025/3/3 14:43:40
                var temp = result[0].Orders.FindAll(c => c.OrderDate >= new DateTime(2025, 3, 3, 15, 0, 0));
            }
            else if (opera == "Add")
            {
                var users = new List<T_User>
                {
                    new T_User
                    {
                        UserName="张三",
                        Orders=new List<T_Order>
                        {
                            new T_Order{ Amount=20,OrderDate=DateTime.Now},
                            new T_Order{ Amount=40,OrderDate=DateTime.Now},
                            new T_Order{ Amount=60,OrderDate=DateTime.Now},
                        }
                    },
                    new T_User
                    {
                        UserName="李四",
                        Orders=new List<T_Order>
                        {
                            new T_Order{ Amount=20,OrderDate=DateTime.Now},
                            new T_Order{ Amount=40,OrderDate=DateTime.Now},
                            new T_Order{ Amount=60,OrderDate=DateTime.Now},
                        }
                    },
                    new T_User
                    {
                        UserName="王五",
                        Orders=new List<T_Order>
                        {
                            new T_Order{ Amount=20,OrderDate=DateTime.Now},
                            new T_Order{ Amount=40,OrderDate=DateTime.Now},
                            new T_Order{ Amount=60,OrderDate=DateTime.Now},
                        }
                    },
                };
                DbHelperService.DbContext.InsertNav(users).Include(it => it.Orders).ExecuteCommand();
                foreach (var user in users)
                    AllUser.Add(user);
            }
            else if (opera == "Update")
            {
                var temp = AllUser.FirstOrDefault(c => c.UserId == 1);
                if (temp == null) return;
                temp.UserName = "张三";
                temp.Orders[0].OrderDate = DateTime.Now.AddHours(1);
                var newUser = DbHelperService.DbContext.UpdateNav(temp).Include(c => c.Orders).ExecuteCommand();
            }
            else if (opera == "Delete")
            {
                var delist = AllUser.Where(c => c.UserId == 1).ToList();
                bool res = DbHelperService.DbContext.DeleteNav(delist).Include(c => c.Orders).ExecuteCommand();
                foreach (var d in delist)
                    AllUser.Remove(d);
            }
            else if (opera == "ClearTable")
            {
                //清空表
                DbHelperService.DbContext.DbMaintenance.TruncateTable<T_User, T_Order>();
            }
            else if (opera == "ExeportExcel")
            {
                var list = AllUser.Select(c => new
                {
                    编号 = c.UserId,
                    姓名 = c.UserName,
                    时间 = c.CreateDateTime
                }).ToList();
                if (EPPlusExcelService.DataToExcel(DataConvertService.ListToDataTable(list)))
                    InfoShowService.Success();

                //if(EPPlusExcelService.ExportBigExcelTest())
                //    InfoShowService.Success();
            }
            else if (opera == "EncryptString")
            {
                var temp = AESService.EncryptString("三大法宝速度快5555999999999三大法宝当升科技");
                LogService.Logger.Info($"加密信息：{temp}");
            }
            else if (opera == "DecryptString")
            {
                var temp = AESService.DecryptString(@"XLs0dcATk5+iaB9efdr339Z7+1jVUcl3a7NNJJI2ZTj2a6ZtwuXlCYQ82OuE/JNLVyhqzmrXlXex1S0+JNWHww==");
                LogService.Logger.Info($"解密信息：{temp}");
            }
            else if (opera == "TCP")
            {
                tcpService.ConnectFailure += (s) =>
                {
                    LogService.Logger.Error("连接失败！");
                };
                tcpService.ConnectAsync("192.168.2.144");
            }
            else if (opera== "D_TCP")
            {
                tcpService.CloseConnect();
            }

            //阻塞直到有可用项
            //BlockingCollection<int> queue = new BlockingCollection<int>();
        }


    }
}
